package com.mazong.serverblog.controller;

import com.mazong.serverblog.mapper.IDbUserMapper;
import com.mazong.serverblog.pojo.TbUser;
import com.mazong.serverblog.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping(value = "/")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IDbUserMapper iDbUserMapper;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUser")
    @ResponseBody
    public Object getUser(@RequestParam("id") Integer id) {
        TbUser user = iDbUserMapper.getUserById(id);

        return user;
    }

    @RequestMapping(value = "/getUserByPage")
    @ResponseBody
    public List<TbUser> getUserByPage(@RequestParam("pageSize") int pageSize,
                                      @RequestParam("pageNo") int pageNo) {
        List<TbUser> users = iDbUserMapper.getUserByPage(pageSize, pageNo);
        if(users != null) {
            logger.info("getUserByPage,count="+users.size());
        }

        return users;
    }

    @RequestMapping(value = "/addUser")
    @ResponseBody
    public Object addUser(@RequestParam("name") String name, @RequestParam("age") int age) {

        // http://localhost:8000/addUser
        TbUser user = new TbUser();
        user.setUserName(name);
        user.setAge(age);
        int id = iDbUserMapper.addUser(user);
        System.out.println("addUser="+user.getId());

        return user;
    }

    /**
     * 增加用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/addUserObj")
    @ResponseBody
    public Object addUserObj(@RequestBody TbUser user) {

        // http://localhost:8000/addUserObj?UserName=huahua&Age=19
        // {"UserName":"huahua","Age":19}
        // {"userName":"huahua","age":19}
        logger.info("user:"+user);
        int id = iDbUserMapper.addUser(user);
        System.out.println("addUser="+user.getId());

        return user;
    }


    @RequestMapping(value = "/delUser")
    @ResponseBody
    public String getUserById(@RequestParam("id") Integer id) {
        // http://localhost:8000/getUserById?userId=2

        iDbUserMapper.delUser(id);
        return String.format("delete id=%d", id);
    }

    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public Object updateUser(@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("age") int age) {

        // http://localhost:8000/updateUser
//        TbUser user = new TbUser();
//        user.setId(id);
//        user.setUserName(name);
//        user.setAge(age);
//        iDbUserMapper.updateUser(user);
//        System.out.println("updateUser="+user.getId());


        return userService.setUser(id, name, age);
    }

    //====================================================
    @RequestMapping(value = "/dbTrans")
    @ResponseBody
    @Transactional
    public Object dbTrans() {
        iDbUserMapper.updateUserName(15, "lisa1");
        logger.info("dbTrans,1");
        //int count = 2/0;
        logger.info("dbTrans,2");

        iDbUserMapper.updateUserName(15, "zhufeifei1");

        return "ok";
    }

    //====================================================

    @RequestMapping(value = "/index")
    public String index(Model model) {
        logger.info("index .......");
        model.addAttribute("name", "jack");
        return "index";
    }

    @RequestMapping(value = "/runerr")
    public String runError(Model model) {
        logger.info("runError .......");

        String msg = null;
        if(msg.length()>0) {
            System.out.println("hello world");
        }

        return "index";
    }
}
